package com.seagull.sb.util;

import java.util.ArrayList;
import java.util.List;

import com.seagull.sb.domain.HomeWork;
import com.seagull.sb.domain.Leave;
import com.seagull.sb.domain.Notice;
import com.seagull.sb.domain.Student;
import com.seagull.sb.domain.Teacher;
import com.seagull.sb.dto.BaseDto;
import com.seagull.sb.dto.HomeWorkDto;
import com.seagull.sb.dto.LeaveDto;
import com.seagull.sb.dto.NoticeDto;
import com.seagull.sb.dto.StudentDto;
import com.seagull.sb.dto.TeacherDto;

public class ServiceMapper {
	private static ServiceMapper sm;
	private static Integer sync=1;
	public static ServiceMapper getInstaMapper(){
		if(sm==null){
			synchronized (sync) {
				sm = new ServiceMapper();
				return sm;
			}
		}
		return sm;
	}
	
	public SBRequestContext buildRequestContext(BaseDto baseDto){
		SBRequestContext rq = new SBRequestContext();
		
		if(baseDto instanceof NoticeDto){
			NoticeDto target = ((NoticeDto) baseDto);
			rq.setClassId(target.getClassId());
			rq.setOrgId(target.getOrgId());
		}
		
		return rq; 
	}
	public List<NoticeDto> mapToNoticeDto(List <Notice> notices){
		
		List<NoticeDto> noticeDto = new ArrayList<NoticeDto>();
		NoticeDto target;
		for (Notice notice : notices) {
			target = new NoticeDto();
			target.setId(notice.getId());
			target.setClassId(notice.getClassId());
			target.setOrgId(notice.getOrgId());
			target.setTitle(notice.getTitle());
			target.setDetails(notice.getDetails());
			target.setNoteType(notice.getNoteType());
			target.setType(notice.getType());
			target.setCreatedBy(notice.getCreatedBy());
			noticeDto.add(target);
		}
		return noticeDto;
	}

	public List<HomeWorkDto> mapToHomeWorkDto(List <HomeWork> homeWorks){
		
		List<HomeWorkDto> homeWorkDto = new ArrayList<HomeWorkDto>();
		HomeWorkDto target;
		for (HomeWork homeWork : homeWorks) {
			target = new HomeWorkDto();
			target.setId(homeWork.getId());
			target.setClassId(homeWork.getClassId());
			target.setOrgId(homeWork.getOrgId());
			target.setSubjectId(homeWork.getSubjectId());
			target.setDetails(homeWork.getDetails());
			target.setTargetDt(homeWork.getTargetDt());
			target.setCreatedBy(homeWork.getCreatedBy());
			target.setCreatedDt(homeWork.getCreatedDt());
			
			homeWorkDto.add(target);
		}
		return homeWorkDto;
	}

	public HomeWorkDto mapToHomeWorkDto(HomeWork homeWork){
		
		HomeWorkDto target = new HomeWorkDto();
		target.setId(homeWork.getId());
		target.setClassId(homeWork.getClassId());
		target.setOrgId(homeWork.getOrgId());
		target.setSubjectId(homeWork.getSubjectId());
		target.setDetails(homeWork.getDetails());
		target.setTargetDt(homeWork.getTargetDt());
		target.setCreatedBy(homeWork.getCreatedBy());
		target.setCreatedDt(homeWork.getCreatedDt());
		
		return target;
	}
	
	public HomeWork mapToHomeWork(HomeWorkDto homeWork){
		HomeWork target = new HomeWork();
		target.setClassId(homeWork.getClassId());
		target.setSubjectId(homeWork.getSubjectId());
		target.setDetails(homeWork.getDetails());
		target.setTargetDt(homeWork.getTargetDt());
			
		return target;
	}
	
	public Notice mapToNotice(NoticeDto noticeDto){
		
			Notice target = new Notice();
			target.setId(noticeDto.getId());
			target.setClassId(noticeDto.getClassId());
			target.setOrgId(noticeDto.getOrgId());
			target.setTitle(noticeDto.getTitle());
			target.setDetails(noticeDto.getDetails());
			target.setNoteType(noticeDto.getNoteType());
			target.setType(noticeDto.getType());
			target.setCreatedBy(noticeDto.getCreatedBy());
			return target;
	}
	
	public List<StudentDto> mapToStudentDto(List <Student> students){
		
		List<StudentDto> studentDto = new ArrayList<StudentDto>();
		StudentDto target;
		for (Student student: students) {
			target = new StudentDto();
			target.setId(student.getId());
			target.setClassId(student.getClassId());
			target.setOrgId(student.getOrgId());
			
			target.setFname(student.getFname());
			target.setLname(student.getLname());
			target.setParentName(student.getParentName());
			target.setGender(student.getGender());
			target.setAddress(student.getAddress());
			target.setPhone(student.getPhone());
			target.setMobilePrimary(student.getMobilePrimary());
			target.setMobileSecondary(student.getMobileSecondary());
			target.setBloodGroup(student.getBloodGroup());
			target.setWeight(student.getWeight());
			target.setHeight(student.getHeight());
			target.setDob(student.getDob());
			target.setEmail(student.getEmail());
			
			studentDto.add(target);
		}
		return studentDto;
	}
	
	public List<TeacherDto> mapToTeacherDto(List <Teacher> teachers){
		
		List<TeacherDto> teacherDto = new ArrayList<TeacherDto>();
		TeacherDto target;
		for (Teacher teacher: teachers) {
			target = new TeacherDto();
			target.setId(teacher.getId());
			target.setClassId(teacher.getClassId());
			target.setOrgId(teacher.getOrgId());
			
			target.setFname(teacher.getFname());
			target.setLname(teacher.getLname());
			
			target.setGender(teacher.getGender());
			target.setAddress(teacher.getAddress());
			target.setPhone(teacher.getPhone());
			target.setMobilePrimary(teacher.getMobilePrimary());
			target.setMobileSecondary(teacher.getMobileSecondary());
			
			target.setDob(teacher.getDob());
			target.setEmail(teacher.getEmail());
			
			teacherDto.add(target);
		}
		return teacherDto;
	}
	
	
	
	public Student mapToStudent(StudentDto student){
		
			Student target = new Student();

			target.setId(student.getId());
			target.setClassId(student.getClassId());
			target.setOrgId(student.getOrgId());
			
			target.setFname(student.getFname());
			target.setLname(student.getLname());
			target.setParentName(student.getParentName());
			target.setGender(student.getGender());
			target.setAddress(student.getAddress());
			target.setPhone(student.getPhone());
			target.setMobilePrimary(student.getMobilePrimary());
			target.setMobileSecondary(student.getMobileSecondary());
			target.setBloodGroup(student.getBloodGroup());
			target.setWeight(student.getWeight());
			target.setHeight(student.getHeight());
			target.setDob(student.getDob());
			target.setEmail(student.getEmail());
			
			return target;
	}	
	
	public List<LeaveDto> mapToLeaveDto(List <Leave> leaves){
		
		List<LeaveDto> leaveDto = new ArrayList<LeaveDto>();
		LeaveDto target;
		for (Leave leave : leaves) {
			target = new LeaveDto();
			
			target.setId(leave.getId());
			target.setOrgId(leave.getOrgId());
			target.setClassId(leave.getClassId());
			target.setStudentId(leave.getStudentId());
			target.setTeacherId(leave.getTeacherId());
			target.setSubject(leave.getSubject());
			target.setStartdt(leave.getStartdt());
			target.setEnddt(leave.getEnddt());
			target.setModifieddt(leave.getModifieddt());
			target.setApproved(leave.getApproved());
			target.setRejectedComments(leave.getRejectedComments());
			target.setModifiedby(leave.getModifiedby());
			
			leaveDto.add(target);
		}
		return leaveDto;
	}
	
	
	public Leave mapToLeave(LeaveDto leaveDto){
		
		Leave target = new Leave();
		
		// below fields are populated from Form details
		target.setSubject(leaveDto.getSubject());
		target.setStartdt(leaveDto.getStartdt());
		target.setEnddt(leaveDto.getEnddt());
		
		return target;
	}
	
	public Leave mapToAuthorizeLeave(LeaveDto leaveDto){
		
		Leave target = new Leave();
		
		// below fields are populated from Form details
		target.setId(leaveDto.getId());
		target.setOrgId(leaveDto.getOrgId());
		target.setClassId(leaveDto.getClassId());
		target.setStudentId(leaveDto.getStudentId());
		
		target.setApproved(leaveDto.getApproved());
		target.setRejectedComments(leaveDto.getRejectedComments());
		target.setModifieddt(leaveDto.getModifieddt());
		
		return target;
	}
	
	
}
